package com.wu.service.impl;

import com.wu.exceptionhandler.GuliException;
import com.wu.utils.JwtUtils;
import com.wu.utils.MD5;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wu.pojo.User;
import com.wu.mappers.UserMapper;
import com.wu.service.UserService;

import java.util.List;

/**
@author sun
@date 2022/3/29 - 23:23
*/
@Service
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<User> findUser(User user) {
        return userMapper.findUser(user);
    }

    @Override
    public String login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
//校验参数
        if(StringUtils.isEmpty(username) ||
                StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"error");
        }
//获取用户信息
        User member = userMapper.findUserOne(user);
        if(null == member) {
            throw new GuliException(20001,"error");
        }
//校验密码
        /*if(!MD5.encrypt(password).equals(member.getPassword())) {
            throw new GuliException(20001,"error");
        }*/
        if(!password.equals(member.getPassword())) {
            throw new GuliException(20001,"error");
        }
//使用JWT生成token字符串
        String token = JwtUtils.getJwtToken(member.getId().toString(), member.getUsername());
        return token;
    }

}
